package cn.edu.cug.cs.gtl.oi.ui;

import cn.edu.cug.cs.gtl.oi.render.Render;
import com.openinventor.inventor.nodes.SoGradientBackground;
import com.openinventor.inventor.nodes.SoNode;
import com.openinventor.inventor.nodes.SoSeparator;
import com.openinventor.inventor.viewercomponents.awt.glcanvas.viewers.ViewerExaminer;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public class RenderPanel extends JPanel {
    MainApp theApp = null;
    Render  render=null;


    public RenderPanel(MainApp theApp,LayoutManager layout, boolean isDoubleBuffered) {
        super(layout, isDoubleBuffered);
        this.render = new Render();
        this.theApp=theApp;
    }

    public RenderPanel(MainApp theApp,LayoutManager layout) {
        super(layout);
        this.render = new Render();
        this.theApp=theApp;
    }

    public RenderPanel(MainApp theApp,boolean isDoubleBuffered) {
        super(isDoubleBuffered);
        this.render = new Render();
        this.theApp=theApp;
    }

    public RenderPanel(MainApp theApp) {
        setLayout(new BorderLayout());
        this.render = new Render();
        this.theApp=theApp;
    }

    public ViewerExaminer getViewer() {
        return this.render.getViewer();
    }

    public SoNode getSceneGraph(){
        return this.render.getSceneGraph();
    }

    public Render getRender() {
        return render;
    }

    public void setRender(Render render) {
        this.render = render;
    }

    public void initial(){
        // open chair.iv
        SoNode sceneGraph =null;
        //sceneGraph = theApp.readSceneGraph("$OIVJHOME/data/models/chair.iv");
        try {
            sceneGraph = theApp.readSceneGraph("D:" + File.separator + "temp" + File.separator + "test.h3d");
        }
        catch (Exception e){
            e.printStackTrace();
        }

        SoSeparator root = new SoSeparator();
        {
            root.addChild(new SoGradientBackground());
            root.addChild(sceneGraph);
        }

        this.render.setSceneGraph(root);
        getViewer().viewAll();

        //getViewer().setPreferredSize(new java.awt.Dimension(800, 600));

        add(getViewer(), BorderLayout.CENTER);
    }

    public void reset(SoNode sceneGraph){
        SoSeparator root = new SoSeparator();
        {
            root.addChild(new SoGradientBackground());
            root.addChild(sceneGraph);
        }

        this.render.setSceneGraph(root);

        getViewer().viewAll();
    }


}
